from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re, types

class CreateUserTest(object):

    def test_create_user(self):
        self.go_to_main_page()
        self.create_user('Ann', 'ann@qwe.qwe', 'qwe', 'Guest (view only)')

    def login_as(self, login, password):
        assert self.waiter.until(lambda drv: drv.find_element_by_link_text("Log in")).is_displayed
        self.driver.find_element_by_link_text("Log in").click()
        userNameField = self.waiter.until(lambda drv: drv.find_element_by_id("username"))
        userNameField.clear()
        userNameField.send_keys(login)
        passwordField = self.waiter.until(lambda drv: drv.find_element_by_name("password"))
        passwordField.clear()
        passwordField.send_keys(password)
        self.waiter.until(lambda drv: drv.find_element_by_name("submit")).click()
        assert self.driver.find_element_by_link_text("Log out")

    def go_to_user_managment(self):
        self.driver.find_element_by_link_text("User management").click()
        assert self.driver.find_element_by_xpath("//*/h2[text()[contains(.,'User management')]]")

    def delete_present_user(self, delete_user):
        try:
            self.driver.find_element_by_xpath("(//td[2]/a[contains(text(), '%s')]/following::*/*[@alt='Remove'])[1]" % delete_user) .click()
            self.waiter.until(lambda drv: drv.switch_to_alert()).accept()
        except NoSuchElementException as e:
            print 'no element user'
            pass

    def go_to_main_page(self):
        self.driver.get(self.base_url + "/php4dvd/")

    def create_user(self, user_name, email, passwd, role):
        self.login_as('admin', 'admin')
        self.go_to_user_managment()
        try:
            self.delete_present_user(user_name)
        except NoSuchElementException as e:
            pass

        count_elements =  len(self.driver.find_elements_by_xpath("//*/td[2]/a[@*]"))
#        print count_elements
        createUserNameField = self.driver.find_element_by_id("username")
        createUserNameField.click()
        createUserNameField.clear()
        createUserNameField.send_keys(user_name)
        emailField = self.driver.find_element_by_name("email")
        emailField.clear()
        emailField.send_keys(email)
        passwordField = self.driver.find_element_by_id("password")
        passwordField.clear()
        passwordField.send_keys(passwd)
        passwordFieldConfirm = self.driver.find_element_by_id("password2")
        passwordFieldConfirm.clear()
        passwordFieldConfirm.send_keys(passwd)
        Select(self.driver.find_element_by_name("permission")).select_by_visible_text(role)
        self.driver.find_element_by_name("submit").click()
        assert len(self.driver.find_elements_by_xpath("//*/td[2]/a[@*]")) > count_elements
        self.logout()

    def logout(self):
        self.waiter.until(lambda drv: drv.find_element_by_link_text("Log out")).click()
        self.waiter.until(lambda drv: drv.switch_to_alert()).accept()
        self.assert_(self.is_on_login_page(), "Page not found!")

    def is_on_login_page(self):
        return self.driver.find_element_by_link_text("Log in")

if __name__ == "__main__":
     unittest.main()